<template>
  <div class="app-main-container">
    <div class="doumee-filter manufacturer">
      <el-button type="primary" @click="addBtn">{{ $t('message.tijia') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table doumee-element-tableb"
    >
      <el-table-column
        align="center"
        :label="$t('message.qymc')"
        prop="name"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="$t('message.zb1')"
        prop="eastLongitude"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="$t('message.zb2')"
        prop="southLatitude"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="$t('message.zb3')"
        prop="westLongitude"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="$t('message.zb4')"
        prop="northLatitude"
        min-width="200"
      />
      <el-table-column
        fixed="right"
        width="1"
      />
      <el-table-column :label="$t('message.glsb')" align="center" min-width="120" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button v-preventReClick type="text" @click="showSbBtn(scope.row)">{{ $t('message.djck') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <el-dialog
      :title="$t('message.add')"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      :visible.sync="showZhpz"
    >
      <el-form ref="ruleForm" label-suffix="：" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('message.qymc')" prop="name">
          <el-input v-model="form.name" :placeholder="$t('message.qsr') + $t('message.qymc')" />
        </el-form-item>
        <el-form-item :label="$t('message.zb1')" prop="eastLongitude">
          <el-col :span="11"><el-input v-model="form.eastLongitude[0]" :placeholder="$t('message.qsr') + $t('message.jindu')" maxlength="50" /></el-col>
          <el-col :span="2" style="text-align: center"><span>-</span></el-col>
          <el-col :span="11"><el-input v-model="form.eastLongitude[1]" :placeholder="$t('message.qsr') + $t('message.weidu')" maxlength="50" /></el-col>
        </el-form-item>
        <el-form-item :label="$t('message.zb2')" prop="southLatitude">
          <el-col :span="11"><el-input v-model="form.southLatitude[0]" :placeholder="$t('message.qsr') + $t('message.jindu')" maxlength="50" /></el-col>
          <el-col :span="2" style="text-align: center"><span>-</span></el-col>
          <el-col :span="11"><el-input v-model="form.southLatitude[1]" :placeholder="$t('message.qsr') + $t('message.weidu')" maxlength="50" /></el-col>
        </el-form-item>
        <el-form-item :label="$t('message.zb3')" prop="westLongitude">
          <el-col :span="11"><el-input v-model="form.westLongitude[0]" :placeholder="$t('message.qsr') + $t('message.jindu')" maxlength="50" /></el-col>
          <el-col :span="2" style="text-align: center"><span>-</span></el-col>
          <el-col :span="11"><el-input v-model="form.westLongitude[1]" :placeholder="$t('message.qsr') + $t('message.weidu')" maxlength="50" /></el-col>
        </el-form-item>
        <el-form-item :label="$t('message.zb4')" prop="northLatitude">
          <el-col :span="11"><el-input v-model="form.northLatitude[0]" :placeholder="$t('message.qsr') + $t('message.jindu')" maxlength="50" /></el-col>
          <el-col :span="2" style="text-align: center"><span>-</span></el-col>
          <el-col :span="11"><el-input v-model="form.northLatitude[1]" :placeholder="$t('message.qsr') + $t('message.weidu')" maxlength="50" /></el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showZhpz = false">{{ $t('message.qx') }}</el-button>
        <el-button v-preventReClick :loading="subLoading" type="primary" @click="onSubmit">{{ $t('message.qr') }}</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="$t('message.chakan')"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      :visible.sync="showSb"
    >
      <div class="doumee-filter">
        <el-select v-model="devType" :placeholder="$t('message.qxz') + $t('message.sblx')" clearable @change="getSbList(1)">
          <el-option v-for="item in devTypeList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-button type="primary" @click="addsbBtn">{{ $t('message.tijia') }}</el-button>
      </div>
      <el-table
        :data="sbList"
        border
        fit
        :header-row-class-name="'table-header'"
        class="doumee-element-table"
      >
        <el-table-column
          align="center"
          :label="$t('message.sblx2')"
          prop="devType"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ typeMethod(scope.row.devType) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('message.mc')"
          prop="devName"
          min-width="150"
        />
        <el-table-column :label="$t('message.caozuo')" align="center" width="200" fixed="right">
          <template slot-scope="scope">
            <div class="flex-cc">
              <el-button v-preventReClick type="text" class="secondColor" @click="sbDelBtn(scope.row.id)">{{ $t('message.del') }}</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex-cc doumee-pagination">
        <Paginations
          :page-size="pageSizes"
          :total="totals"
          :integrated="integrated"
          :page="page"
          @current-change="getSbList"
        />
      </div>
    </el-dialog>
    <el-dialog
      :title="$t('message.tjsb')"
      :visible.sync="showAddSb"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      width="500px"
    >
      <el-form ref="ruleFormb" label-suffix="：" :model="sbform" :rules="rules" label-width="auto">
        <el-form-item :label="$t('message.sblx')" prop="devType">
          <el-select v-model="sbform.devType" :placeholder="$t('message.qxz') + $t('message.sblx')" @change="getdevList">
            <el-option v-for="item in devTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('message.shibei')" prop="devIds">
          <el-checkbox-group v-model="sbform.devIds">
            <el-checkbox v-for="item in devList" :key="item.devId" :label="item.devId">{{ item.devName }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddSb = false">{{ $t('message.qx') }}</el-button>
        <el-button v-preventReClick :loading="subLoading" type="primary" @click="onSubmitb">{{ $t('message.qr') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addFreeArea, addFreeAreaDevBind, deleteFreeAreaDevBind, getFreeAreaBindDevList, getFreeAreaDevBindList, getFreeAreaList } from '@/api'
import { parseTime, newRule } from '@/utils'
import store from '@/store'
export default {
  name: 'LowriskArea',
  data() {
    return {
      modalDialog: store.getters.dialog,
      subLoading: false,
      pickerOptions: {
        disabledDate(time) {
          return (
            time.getTime() < new Date(new Date().toLocaleDateString()).getTime()
          )
        }
      },
      pageSizes: 6,
      integrated: false,
      title: '',
      tabList: [],
      active: '0',
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      list: [],
      listLoading: true,
      filters: {},
      form: {
        name: '',
        eastLongitude: [],
        southLatitude: [],
        northLatitude: [],
        westLongitude: []
      },
      showSb: false,
      showZhpz: false,
      sbList: [],
      rules: {
        name: [
          { required: true, message: this.$t('message.qsr') + this.$t('message.qymc'), trigger: 'blur' }
        ],
        eastLongitude: [
          { required: true, validator: newRule, trigger: ['change', 'change'] }
        ],
        southLatitude: [
          { required: true, validator: newRule, trigger: ['change', 'change'] }
        ],
        northLatitude: [
          { required: true, validator: newRule, trigger: ['change', 'change'] }
        ],
        westLongitude: [
          { required: true, validator: newRule, trigger: ['change', 'change'] }
        ],
        devType: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.sblx2'), trigger: 'change' }
        ],
        devIds: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.shibei'), trigger: 'change' }
        ]
      },
      totals: 0,
      page: 1,
      id: '',
      devType: '',
      showAddSb: false,
      devList: [],
      devTypeList: [
        { label: this.$t('message.smsxt'), value: '0' },
        { label: this.$t('message.afsxt'), value: '1' },
        { label: this.$t('message.menjin'), value: '2' }
      ],
      sbform: {
        devIds: []
      }
    }
  },
  created() {
    this.title = this.$route.meta.title
    this.getList(1)
  },
  activated() {
    this.$refs.table.doLayout()
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    typeMethod(status) {
      const obj = this.devTypeList.find(item => item.value === status)
      if (obj) {
        return obj.label
      } else {
        return '-'
      }
    },
    sbDelBtn(id) {
      const that = this
      this.$confirm(this.$t('message.sctc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning',
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== 'confirm') {
            close()
            return
          }
          ctx.confirmButtonLoading = true
          deleteFreeAreaDevBind({ param: { id }}).then(res => {
            ctx.confirmButtonLoading = false
            close()
            if (res.errorCode === '000000') {
              that.$message.success(this.$t('message.delcg'))
              that.getSbList(1)
            }
          })
            .catch(() => { ctx.confirmButtonLoading = false })
        }
      }).finally(() => {})
    },
    getdevList() {
      if (this.sbform.devType) {
        getFreeAreaBindDevList({
          param: {
            id: this.id,
            devType: this.sbform.devType
          }
        }).then(res => {
          if (res.errorCode === '000000') {
            this.devList = res.recordList
            this.$set(this.sbform, 'devIds', [])
          }
        }).catch(err => {
          console.log(err)
        })
      }
    },
    addsbBtn(e) {
      this.showAddSb = true
      this.showSb = false
      this.devList = []
      this.sbform = {
        devIds: []
      }
    },
    showSbBtn(e) {
      this.showSb = true
      this.id = e.id
      this.devType = ''
      this.getSbList(1)
    },
    getSbList(page) {
      if (page) {
        this.page = page
      }
      getFreeAreaDevBindList({
        pagination: {
          page: this.page,
          rows: this.pageSizes,
          firstQueryTime: parseTime(new Date())
        },
        param: {
          id: this.id,
          devType: this.devType
        }
      }).then(res => {
        if (res.errorCode === '000000') {
          this.sbList = res.recordList
          this.totals = res.totalCount
        }
      }).catch(err => {
        console.log(err)
      })
    },
    addBtn() {
      this.showZhpz = true
      this.form = {
        name: '',
        eastLongitude: [],
        southLatitude: [],
        northLatitude: [],
        westLongitude: []
      }
    },
    onSubmitb() {
      const that = this
      this.$refs.ruleFormb.validate((valid) => {
        if (valid) {
          const param = that.sbform
          param.venueId = sessionStorage.getItem('venueId')
          param.id = that.id
          this.subLoading = true
          addFreeAreaDevBind({
            param
          }).then(res => {
            this.subLoading = false
            if (res.errorCode === '000000') {
              that.showAddSb = false
              that.$message.success(this.$t('message.tjcg2'))
              that.showSb = true
              that.devType = ''
              that.getSbList(1)
            }
          }).catch(err => {
            this.subLoading = false
            console.log(err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    onSubmit() {
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const param = { ...that.form }
          param.venueId = sessionStorage.getItem('venueId')
          param.eastLongitude = param.eastLongitude.join(',')
          param.southLatitude = param.southLatitude.join(',')
          param.northLatitude = param.northLatitude.join(',')
          param.westLongitude = param.westLongitude.join(',')
          this.subLoading = true
          addFreeArea({
            param
          }).then(res => {
            this.subLoading = false
            if (res.errorCode === '000000') {
              that.$message.success(this.$t('message.tjcg2'))
              that.showZhpz = false
              that.getList(1)
            }
          }).catch(err => {
            this.subLoading = false
            console.log(err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getList(page) {
      const { filters, pagination } = this
      const venueId = sessionStorage.getItem('venueId')
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      getFreeAreaList({ pagination, param: { venueId, ...filters }}, sessionStorage.getItem('venueId')).then(res => {
        this.listLoading = false
        if (res.errorCode !== '000000') return
        this.list = res.recordList
        this.total = res.totalCount
        this.$refs.table.doLayout()
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.footer-style {
  display: flex;
  justify-content: space-between;
  background-color: #f2f2f2;
  margin: -10px -20px -30px;
  height: 50px;
  .bottom-left {
    margin-left: 20px;
    height: 20px;
    margin-top: 15px;
    .sum-title-style {
      font-size: 15px;
      color: #222;
    }
    .sum-pay-value-style {
      font-size: 17px;
      font-weight: 500;
      color: #dd0707;
    }
    .dis-title-style {
      font-size: 14px;
      color: #222;
    }
    .discoun-style {
      font-size: 16px;
      color: #dd0707;
    }
  }
  .bottom-right {
    margin-right: 20px;
    margin-top: 10px;
    height: 30px;
    .pay-action-style {
      width: 100px;
      height: 30px;
      font-size: 15px;
      line-height: 1px;
      vertical-align: middle;
    }
  }
}
.tips{
  display: flex;
  margin-bottom: 20px;
  .tips_box{
    flex: 1;
    display: flex;
    align-items: center;
    background-color: rgba(32, 178, 170, 1);
    &:first-of-type{
      margin-right: 10px;
    }
    &:last-of-type{
      margin-left: 10px;
      background-color: rgba(65, 105, 225, 1);
    }
    div {
      font-size: 20px;
      color: #fff;
      text-align: center;
      padding: 20px 0;
      line-height: 1.5;
      flex: 1;
      p{
        margin: 0;
        &:last-of-type{
          margin-top: 20px;
          font-size: 24px;
          font-weight: bold;
        }
      }
      &.tips_xian{
        width: 1px;
        height: 80px;
        background-color: #fff;
        flex: none;
      }
    }
  }
}
</style>
